package com.cloud.system.api.mapper;

import java.util.Date;
import java.util.List;

import com.cloud.system.api.bean.HumanDetail;
import com.cloud.system.api.bean.HumanInfoCriteriaBean;
import com.cloud.system.api.bean.HumanInfoDetail;
import com.cloud.system.api.bean.PatrolCriteriaBean;
import com.cloud.system.api.provider.HumanInfoPageProvider;
import com.cloud.system.api.provider.PatrolPageProvider;
import com.cloud.system.common.result.SelectItem;
import com.sun.tools.corba.se.idl.toJavaPortable.Helper24;
import org.apache.ibatis.annotations.*;

import com.cloud.system.api.entity.HumanInfo;
import com.cloud.system.common.driver.SimpleInlangDriver;
import com.cloud.system.common.utils.BaseMapperHelper;

public interface HumanInfoMapper extends BaseMapperHelper<HumanInfo> {
	@Select("select unit_id from human_info where delete_flag = 0 and human_type = #{humanType} and unit_id in (#{unitIds}) group by unit_id having count(1) > 0")
	@Lang(SimpleInlangDriver.class)
	@ResultType(value = Long.class)
	List<Long> selectUnitHumanCount(@Param("humanType")String humanType,@Param("unitIds") List<Long> uids);

	@Select("select unit_id from human_info where delete_flag = 0 and human_type = #{humanType} and unit_id in (#{unitIds}) and unit_id in(select unit_id from unit_domain where sub_domain_type_id in (#{doMainList}))   group by unit_id having count(1) > 0")
	@Lang(SimpleInlangDriver.class)
	@ResultType(value = Long.class)
	List<Long> selectUnitHumanCountByDomain(@Param("humanType")String humanType, @Param("unitIds") List<Long> uids, @Param("doMainList") List<String> doMainList);

	@Select("select i.human_id, human_name, unit_id, unit_name, unit_code from human_info i where delete_flag = 0 "
			+ "and exists (select human_id from human_role h where i.human_id = h.human_id and role_id = #{roleId})")
	@Results({ @Result(property = "humanId", column = "human_id", javaType = Long.class),
			@Result(property = "humanName", column = "human_name", javaType = String.class),
			@Result(property = "unitId", column = "unit_id", javaType = Long.class), 
			@Result(property = "unitName", column = "unit_name", javaType = String.class),
			@Result(property = "unitCode", column = "unit_code", javaType = String.class)})
	List<HumanInfo> findHumanByRole(long roleId);

	@Select("select count(*) from human_info where user_name = #{userName}")
	@ResultType(Integer.class)
	int countByUserNameAndHumanId(@Param("userName")String userName);


	@SelectProvider(type = PatrolPageProvider.class, method = "listPage")
	@Results({
			@Result(property = "humanId",column = "human_id",javaType = Long.class),
			@Result(property = "humanName", column = "human_name", javaType = String.class),
			@Result(property = "unitName", column = "unit_name", javaType = String.class),
			@Result(property = "positionLevel", column = "position_level", javaType = String.class),
			@Result(property = "humanNature", column = "human_nature", javaType = String.class),
			@Result(property = "occupFlagValue", column = "occup_flag", javaType = String.class),
			@Result(property = "lawcardFlagValue", column = "lawcard_flag", javaType = String.class),
	})
	List<HumanInfo> listPage(PatrolCriteriaBean criteria);

	@Select("select * from human_info where unit_id = #{unitId} and is_law_human = 1 and human_type != '00' and delete_flag != 1 order by update_time desc")
    @Results({
            @Result(property = "humanId",column = "human_id",javaType = Long.class),
            @Result(property = "humanName", column = "human_name", javaType = String.class),
            @Result(property = "identityNum", column = "identity_num", javaType = String.class),
            @Result(property = "lawCardNum", column = "law_card_num", javaType = String.class),
    })
	List<HumanInfo> loadStaffsByUnitId(@Param("unitId")Long unitId,@Param("humanType")String humanType);

	@SelectProvider(type = PatrolPageProvider.class, method = "noDomainsListPage")
	@Results({
			@Result(property = "humanId",column = "human_id",javaType = Long.class),
			@Result(property = "humanName", column = "human_name", javaType = String.class),
			@Result(property = "unitName", column = "unit_name", javaType = String.class),
			@Result(property = "positionLevel", column = "position_level", javaType = String.class),
			@Result(property = "humanNature", column = "human_nature", javaType = String.class),
			@Result(property = "occupFlagValue", column = "occup_flag", javaType = String.class),
			@Result(property = "lawcardFlagValue", column = "lawcard_flag", javaType = String.class),
	})
	List<HumanInfo> noDomainsListPage(PatrolCriteriaBean criteria);

	@Select("select human_id as value, human_name as label, law_card_num as title from human_info where human_type='01' and unit_id = #{unitId} and delete_flag != 1 ")
	@Results({ @Result(property = "value", column = "value", javaType = String.class),
			@Result(property = "label", column = "label", javaType = String.class),
			@Result(property = "title", column = "title", javaType = String.class),})
	List<SelectItem> getHumans(@Param("unitId") Long unitId);

	@Select("delete from human_info where unit_id = #{unitId} and human_type = '00'")
	void deleteUnitAdminByUnitId(Long unitId);

	@Select("select Human_id,unit_id,unit_name from human_info where (human_name = #{humanName} and law_card_num = #{LawCardNum} and delete_flag != 1) or (identity_num = #{identityNum} and delete_flag != 1)")
	@Results({ @Result(property = "humanId", column = "Human_id", javaType = Long.class),
			@Result(property = "unitId", column = "unit_id", javaType = Long.class),
			@Result(property = "unitName", column = "unit_name", javaType = String.class),})
	List<HumanInfo> selectHumanByMameAndLawCard(@Param("humanName") String HumanName, @Param("LawCardNum") String lawCardNum, @Param("identityNum") String identityNum);

	@Select("select Human_id,user_name,human_password,login_flag,display_order,is_law_human,new_add_flag,uuid,valid_status,create_human_id,create_human,create_time from human_info where human_id = #{humanId} and delete_flag != 1")
	@Results({ @Result(property = "humanId", column = "Human_id", javaType = Long.class),
			@Result(property = "userName", column = "user_name", javaType = String.class),
			@Result(property = "humanPassword", column = "human_password", javaType = String.class),
			@Result(property = "loginFlag", column = "login_flag", javaType = Integer.class),
			@Result(property = "displayOrder", column = "display_order", javaType = Integer.class),
			@Result(property = "isLawHuman", column = "is_law_human", javaType = Integer.class),
			@Result(property = "newAddFlag", column = "new_add_flag", javaType = Integer.class),
			@Result(property = "uuid", column = "uuid", javaType = String.class),
			@Result(property = "validStatus", column = "valid_status", javaType = String.class),
			@Result(property = "createHumanId", column = "create_human_id", javaType = Long.class),
			@Result(property = "createHuman", column = "create_human", javaType = String.class),
			@Result(property = "createTime", column = "create_time", javaType = Date.class),})
    HumanInfoDetail selectInfoById(@Param("humanId") Long humanId);

	@SelectProvider(type = HumanInfoPageProvider.class, method = "listHumanInfoPage")
	@Results({ @Result(property = "humanId", column = "human_id", javaType = Long.class),
			@Result(property = "unitId", column = "unit_id", javaType = Long.class),
			@Result(property = "unitName", column = "unit_name", javaType = String.class),
			@Result(property = "userName", column = "user_name", javaType = String.class),
			@Result(property = "humanPassword", column = "human_password", javaType = String.class), })
	List<HumanInfo> listHumanInfoPage(HumanInfoCriteriaBean humanInfoCriteriaBean);

	@Select("select human_id from human_info where unit_id = #{unitId} and human_type = '00' and delete_flag != 1")
	@Results({@Result(property = "humanId", column = "Human_id", javaType = Long.class)})
	HumanDetail getUnitAdministratorByUnitId(@Param("unitId") Long unitId);

	@Insert("INSERT INTO `cda`.`human_info`(`human_id`, `user_name`, `human_password`, `human_name`, `identity_num`, `gender`, `birthday`, `political_status`, `position_level`, `nation`, `education`, `education_major`, `human_nature`, `human_type`, `region`, `domain`, `occup_flag`, `lawcard_flag`, `lawyer_flag`, `telephone`, `law_card_num`, `law_card_type`, `issue_date`, `effective_start`, `effective_end`, `issue_subject_name`, `unit_id`, `unit_name`, `unit_code`, `login_flag`, `delete_flag`, `display_order`, `organ_id`, `organ_name`, `is_confirm`,  `is_law_human`, `new_add_flag`, `uuid`,  `create_human_id`, `create_human`, `create_time`) VALUES " +
			                              "(#{humanId}, #{userName}, #{humanPassword}, #{humanName}, #{identityNum}, #{gender}, #{birthday}, #{politicalStatus}, #{positionLevel}, #{nation}, #{education}, #{educationMajor}, #{humanNature}, #{humanType}, #{region}, #{domain}, #{occupFlag}, #{lawcardFlag}, #{lawyerFlag}, #{telephone}, #{lawCardNum}, #{lawCardType}, #{issueDate}, #{effectiveStart}, #{effectiveEnd}, #{issueSubjectName}, #{unitId}, #{unitName},#{unitCode}, #{loginFlag}, #{deleteFlag}, #{displayOrder}, #{organId}, #{organName}, #{isConfirm}, #{isLawHuman}, #{newAddFlag}, #{uuid}, #{createHumanId}, #{createHuman}, #{createTime})")
	@Options(useGeneratedKeys = true,keyProperty = "humanId")
	int add(HumanInfo humanInfo);

}
